#!/bin/sh
###===================================================
### Updated: 11/18/2007
###===================================================
### Run - Script for building kernel image 
###===================================================
### THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
### "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
### LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
### A PARTICULAR PURPOSE ARE DISCLAIMED.
###
### See LICENSE.OCTANE for more details

CUR_DIR=`pwd`

. ${CUR_DIR}/SpiritConfig 

MAX_SIZE=410

LOOPDEV=/dev/loop0
IMGDIR=${CUR_DIR}/tmp
IMGFILE=${IMGDIR}/orange.img
MNTDIR=${CUR_DIR}/mnt
BOCHS_DIR=${CUR_DIR}/bochs

# Assuming everything went well,
#  the kernel file should exist in the parent directory
KERNELFILE=${CUR_DIR}/vmlinuz
GRUB_FILE=${CUR_DIR}/grub-0.94/menu.lst

STAGE1_FILE=${CUR_DIR}/grub-0.94/stage1
STAGE2_FILE=${CUR_DIR}/grub-0.94/stage2

DISKSIZE=1440

case "`/usr/bin/env id`" in
	"uid=0("*) ;;
	*) echo >&2 "Error: Invalid permissions, run sudo: sudo ./Run"; exit 1;;
esac

echo "====================================="
echo "Running...."
echo "====================================="
echo
echo "====================================="
echo " NOTE: SpiritLite Version		"
echo " You must change your CUR_DIR	"
echo " ++ Currently: $CUR_DIR"

# Experimental, may have to remove

mkdir -p mnt
chown bigbinc mnt
chgrp bigbinc mnt
mkdir -p bochs
chown bigbinc bochs
chgrp bigbinc bochs
mkdir -p tmp
chown bigbinc tmp
chgrp bigbinc tmp

# Create the image file
dd if=/dev/zero of=$IMGFILE bs=1k count=$DISKSIZE

echo "====================================="
echo "Preparing loop back device..."
echo "====================================="

# Prepare the image for loop back device thing
if ! `losetup $LOOPDEV $IMGFILE` ; then
	echo "losetup failed..."
	echo "Please make sure that you are root"
	echo "Also make sure that you have loop device compiled in the kernel."

	rm -f $IMGFILE
	exit 0
fi

echo "====================================="
echo "Creating ext2fs..."
echo "====================================="

echo "Creating ext2 fs..."
mke2fs -m 0 $LOOPDEV
if [ "$?" != "0" ] ; then
	echo "ERROR $? Making EXT2 FS"
	exit 1;
fi

echo "====================================="
echo "Mounting the loopdevice...."
echo "====================================="

mount $LOOPDEV $MNTDIR -t ext2
if [ "$?" != "0" ] ; then
	echo "ERROR $? mounting loop device."
	losetup -d $LOOPDEV
	exit 1;
fi

if [ ! -f $KERNELFILE ] ; then
	echo "The linux kernel is missing, exiting..."

	echo "(cleanup)Performing cleanup, exiting..."
	umount $MNTDIR
	losetup -d $LOOPDEV
	exit 1

fi

# Check for the filesize
total_size=$(du -c $KERNELFILE | tail -1 | cut -f1);

echo "TOTAL KERNEL SIZE: $total_size kb"

if [ $total_size -gt $MAX_SIZE ] ; then
	echo "** error: Your kernel is too large!"
	echo "** You can fix this by compiling the kernel"
	echo "** with almost nothing."
	echo "** or you change"
	echo "** the MAX_SIZE, which I advise against."
	echo "** exiting in failure"


	echo "(cleanup)Performing cleanup, exiting..."
	umount $MNTDIR
	losetup -d $LOOPDEV
	exit 1

fi

echo "Creating ROOT directory on loop device..."
mkdir -p ${MNTDIR}/bin
mkdir -p ${MNTDIR}/boot
mkdir -p ${MNTDIR}/dev
mkdir -p ${MNTDIR}/etc
mkdir -p ${MNTDIR}/lib
mkdir -p ${MNTDIR}/mnt
mkdir -p ${MNTDIR}/proc
mkdir -p ${MNTDIR}/sbin
mkdir -p ${MNTDIR}/usr
mkdir -p ${MNTDIR}/boot/grub
mkdir -p ${MNTDIR}/usr/bin
mkdir -p ${MNTDIR}/usr/i386-linux-uclibc
mkdir -p ${MNTDIR}/usr/i386-linux-uclibc/lib
mkdir -p ${MNTDIR}/etc/init.d
mkdir -p ${MNTDIR}/etc/rc.d

rm -Rf ${MNTDIR}/lost+found

echo "====================================="
echo "Copying file section [begin]"
echo "====================================="

echo "====================================="
echo "Copying...$KERNELFILE"
echo "====================================="

if ! cp -a $KERNELFILE $MNTDIR ; then
	echo "Error: copy error exiting..."
	umount $MNTDIR
	losetup -d $LOOPDEV
	exit $?
fi

echo "====================================="
echo "Copying...$STAGE1_FILE"
echo "====================================="

if ! cp -a $STAGE1_FILE ${MNTDIR}/boot/grub/stage1 ; then
	echo "Error: copy error exiting..."
	umount $MNTDIR
	losetup -d $LOOPDEV
	exit $?
fi

echo "====================================="
echo "Copying...$STAGE2_FILE"
echo "====================================="

if ! cp -a $STAGE2_FILE ${MNTDIR}/boot/grub/stage2 ; then
	echo "Error: copy error exiting..."
	umount $MNTDIR
	losetup -d $LOOPDEV
	exit $?
fi

echo "====================================="
echo "Copying...$GRUB_FILE"
echo "====================================="

if ! cp -a $GRUB_FILE ${MNTDIR}/boot/grub/menu.lst ; then
	echo "Error: copy error exiting..."
	umount $MNTDIR
	losetup -d $LOOPDEV
	exit $?
fi

###
### Run the Device Creation Script
###
if ! ./gdev ; then
	echo "Device Creation failed [ exiting ]"
	umount $MNTDIR
        losetup -d $LOOPDEV
        exit $?
	
fi

# End Device Creation 

echo "====================================="
echo "Running grub section..."
echo "====================================="

${grub_bin} --batch --device-map=/dev/null << EOF
device (fd0) $LOOPDEV
root (fd0)
setup (fd0)
quit
EOF

#================================================
# Complete image creation 
#================================================
echo "Closing down system..."
umount $MNTDIR
losetup -d $LOOPDEV

echo "Waiting until is everything is written..."
sync

if ! cp -a $IMGFILE $BOCHS_DIR ; then
	echo "Error: copy error exiting..."
fi

echo "done"
echo "**"
echo "** [ Final Steps ] Enter the 'bochs' directory and make sure "
echo "**  that the 'orange.img' binary file exists "
echo "**  then run the bochs application on this image file "
echo "**"

